//
//  Vector3.c
//  3DMathSDK
//
//  Created by admin on 16/7/5.
//  Copyright © 2016年 douyu. All rights reserved.
//

#include "Vector3.h"
#include <math.h>

const Vector3 kVector3Zero = {0, 0, 0};
const Vector3 kVector3AxisX = {1, 0, 0}; // X轴
const Vector3 kVector3AxisY = {0, 1, 0}; // Y轴
const Vector3 kVector3AxisZ = {0, 0, 1}; // Z轴

// 两个向量是否相等
bool isVectorEqual(Vector3 vector1, Vector3 vector2)
{
  return (vector1.x==vector2.x && vector1.y==vector2.y && vector1.z==vector2.z);
}

// 向量加数字
Vector3 plusNum(Vector3 vector, float number)
{
  vector.x += number;
  vector.y += number;
  vector.z += number;
  return vector;
}

// 取模
float modulo(Vector3 vector)
{
  return sqrtf(powf(vector.x, 2) + powf(vector.y, 2) + powf(vector.z, 2));
}

// 向量乘数字
Vector3 multiplyNum(Vector3 vector, float number)
{
  vector.x *= number;
  vector.y *= number;
  vector.z *= number;
  return vector;
}

// 标准化
Vector3 normalize(Vector3 vector)
{
  if (isVectorEqual(vector, kVector3Zero)) {
    // 零向量不能标准化
    return vector;
  }
  float moduloNum = modulo(vector);
  vector = multiplyNum(vector, 1.f/moduloNum);
  return vector;
}

// 向量相加
Vector3 plus(Vector3 vector1, Vector3 vector2)
{
  vector1.x += vector2.x;
  vector1.y += vector2.y;
  vector1.z += vector2.z;
  return vector1;
}

// 向量相减
Vector3 minus(Vector3 vector1, Vector3 vector2)
{
  vector1.x -= vector2.x;
  vector1.y -= vector2.y;
  vector1.z -= vector2.z;
  return vector1;
}

// 向量距离
float distance(Vector3 vector1, Vector3 vector2)
{
  return modulo(minus(vector2, vector1));
}

// 向量点乘
float dotProduct(Vector3 vector1, Vector3 vector2)
{
  return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z;
}

// 向量叉乘
Vector3 crossProduct(Vector3 vector1, Vector3 vector2)
{
  Vector3 ret =
  {
    vector1.y*vector2.z - vector1.z*vector2.y,
    vector1.z*vector2.x - vector1.x*vector2.z,
    vector1.x*vector2.y - vector1.y*vector2.x
  };
  return ret;
}

